package homeworks.cjt2010.homework1;

public final class Ticket {
	// --- Instance Variables --------------------------------------------------
	private final Flight flight;
	private final double price;
	private final FlightClass flightClass;
	private final int seatNumber;
	private final Passenger person;

	// --- Constructors --------------------------------------------------------
	public Ticket(Flight flight, double price, FlightClass flightClass,
			int seatNumber, Passenger person) {
		super();
		this.flight = flight;
		this.price = price;
		this.flightClass = flightClass;
		this.seatNumber = seatNumber;
		this.person = person;
	}

	// --- Getters and Setters -------------------------------------------------
	public Flight getFlight() {
		return flight;
	}

	public double getPrice() {
		return price;
	}

	public FlightClass getFlightClass() {
		return flightClass;
	}

	public int getSeatNumber() {
		return seatNumber;
	}

	public Passenger getPerson() {
		return person;
	}

	// --- Methods -------------------------------------------------------------

	/**
	 * In calculation of hashcode we use the 2 fields person and flight because
	 * they take part in construction of equals method()
	 */
	@Override
	public int hashCode() {

		return person.hashCode() ^ flight.hashCode();
	}

	/**
	 * In calculation of ticket equality we use its 2 fileds
	 * <code>person </code>and <code> flight</code> because they define it
	 * uniquely
	 */
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Ticket)) {
			throw new IllegalArgumentException("Incorrect type of input"
					+ " parameter in equals() method of Ticket class");
		}
		Ticket inputTicket = (Ticket) obj;
		return person.equals(inputTicket.getPerson())
				&& flight.equals(inputTicket.getFlight());
	}

	// work version of toString()
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(100);
		sb.append("Ticket: ");
		sb.append(flight + ", ");
		sb.append(person);
		return sb.toString();
	}

	// @Override
	// public String toString() {
	// StringBuilder sb = new StringBuilder(100);
	// sb.append("Ticket: ");
	// sb.append("flight= ").append(flight);
	// sb.append(", price= ").append(price);
	// sb.append(", flightClass= ").append(flightClass);
	// sb.append(", seatNumber= ").append(seatNumber);
	// sb.append(", person= ").append(person);
	// return sb.toString();
	// }

}
